package MovieGuessingGame.confirmRegistration;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import MovieGuessingGame.login.Owasp;

public class ConfirmDB {
	private String framework = "derbyclient";
	private String driver = "org.apache.derby.jdbc.ClientDriver";
	private String protocol = "jdbc:derby://localhost:1527/";
 
 public static void main(String[] args)
 {
     new ConfirmDB().confirmRegistration("user1","442390822","mypassy", "K1ck1ForMe");
     System.out.println("Lookup in DB finished");
 }

 String confirmRegistration(String userName, String regCode, String password, String dbPass)
 {
     System.out.println("Start of confirmRegistration");
     String retStr = "";
     loadDriver();

     Connection conn = null;

     try
     {
         Properties props = new Properties();
         
         // Info to log into the Database...
         props.put("user", "DBUser");
         props.put("password", dbPass);

         String dbName = "loginDB"; // the name of the database
         Owasp loginDB = new Owasp();
         conn = DriverManager.getConnection(protocol + dbName + ";create=false", props);

         System.out.println("Connected to and created database " + dbName);

         conn.setAutoCommit(false);

         try {
        	 retStr = loginDB.verifyUser(conn, userName, regCode);
        	 System.out.println("Message returned from verify user: " + retStr );
        	 if (retStr.indexOf("OK~") >= 0) {
        		 String[] items = retStr.split("~");
        		 String salt = items[1];
        		 retStr = loginDB.updatePassword(conn, userName, regCode, password, salt);
            	 System.out.println("Message returned from updatePassword: " + retStr );
        	 }

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

         if (framework.equals("embedded"))
         {
             try
             {
                 // the shutdown=true attribute shuts down Derby
                 DriverManager.getConnection("jdbc:derby:;shutdown=true");
             }
             catch (SQLException se)
             {
                 if (( (se.getErrorCode() == 50000)
                         && ("XJ015".equals(se.getSQLState()) ))) {
                     System.out.println("Derby shut down normally");
                 } else {
                     System.err.println("Derby did not shut down normally");
                     printSQLException(se);
                 }
             }
         }
     }
     catch (SQLException sqle)
     {
         printSQLException(sqle);
     } finally {
         //Connection
         try {
             if (conn != null) {
                 conn.close();
                 conn = null;
             }
         } catch (SQLException sqle) {
             printSQLException(sqle);
         }
     }
     return retStr;
 }

 /**
  * Loads the appropriate JDBC driver for this environment/framework. For
  * example, if we are in an embedded environment, we load Derby's
  * embedded Driver, <code>org.apache.derby.jdbc.EmbeddedDriver</code>.
  */
 private void loadDriver() {
     try {
         Class.forName(driver).newInstance();
         System.out.println("Loaded the appropriate driver");
     } catch (ClassNotFoundException cnfe) {
         System.err.println("\nUnable to load the JDBC driver " + driver);
         System.err.println("Please check your CLASSPATH.");
         cnfe.printStackTrace(System.err);
     } catch (InstantiationException ie) {
         System.err.println(
                     "\nUnable to instantiate the JDBC driver " + driver);
         ie.printStackTrace(System.err);
     } catch (IllegalAccessException iae) {
         System.err.println(
                     "\nNot allowed to access the JDBC driver " + driver);
         iae.printStackTrace(System.err);
     }
 }

 private void reportFailure(String message) {
     System.err.println("\nData verification failed:");
     System.err.println('\t' + message);
 }

 /**
  * Prints details of an SQLException chain to <code>System.err</code>.
  * Details included are SQL State, Error code, Exception message.
  *
  * @param e the SQLException from which to print details.
  */
 public static void printSQLException(SQLException e)
 {
     // Unwraps the entire exception chain to unveil the real cause of the
     // Exception.
     while (e != null)
     {
         System.err.println("\n----- SQLException -----");
         System.err.println("  SQL State:  " + e.getSQLState());
         System.err.println("  Error Code: " + e.getErrorCode());
         System.err.println("  Message:    " + e.getMessage());
         // for stack traces, refer to derby.log or uncomment this:
         //e.printStackTrace(System.err);
         e = e.getNextException();
     }
 }
}